using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
	public class CompanyRepository : ICompanyRepository, IDisposable
	{
		private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

		public CompanyRepository(dbVoiceAdDataContext db)
		{
			this.context = db;
		}

		public IEnumerable<tblCompany>GetListCompanys()
		{
			return context.tblCompanies.ToList();
		}

		public tblCompany GetCompanyById(int id)
		{
            return context.tblCompanies.Where(x => x.Id == id).FirstOrDefault();
		}

		public void InsCompany(tblCompany tblcompany)
		{
            context.tblCompanies.InsertOnSubmit(tblcompany);
			context.SubmitChanges();
		}

		public void UpdCompany(tblCompany tblcompany)
		{
            tblCompany obj = context.tblCompanies.Where(x => x.Id == tblcompany.Id).First();
			obj.CompanyName_Vn = tblcompany.CompanyName_Vn;
			obj.CompanyName_En = tblcompany.CompanyName_En;
			obj.Address_Vn = tblcompany.Address_Vn;
			obj.Address_En = tblcompany.Address_En;
			obj.TaxCode = tblcompany.TaxCode;
			obj.Telephone = tblcompany.Telephone;
			obj.Fax = tblcompany.Fax;
			context.SubmitChanges();
		}

		public void DelCompany(int id)
		{
            tblCompany obj = context.tblCompanies.Single(x => x.Id == id);
            context.tblCompanies.DeleteOnSubmit(obj);
			context.SubmitChanges();
		}

		private bool disposed = false;

		protected virtual void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				if (disposing)
				{
					context.Dispose();
				}
			}
			this.disposed = true;
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}
	}
}
